<template>
    <el-row class="login-container">
        <el-col :lg="16" :md="12" class="left">
            <div>
                <div class="font-bold text-light-50 mb-4 text-5xl">欢迎光临</div>
                <div class="text-gray-200 text-sm">此站点是《vue3 + vite实战商城后台开发》视频课程的演示地址</div>
            </div>
        </el-col>
        <el-col :lg="8" :md="12" class="right">
            <h2 class="font-bold text-3xl text-gray-800">欢迎回来</h2>
            <div class="flex justify-center items-center my-5 text-gray-300 space-x-2">
                <span class="h-[1px] w-16 bg-gray-200"></span>
                <span>账号密码登陆</span>
                <span class="h-[1px] w-16 bg-gray-200"></span>
            </div>
            <el-form ref="formRef" :model="form" :rules="rules" class="w-[250px]">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名">
                        <template #prefix>
                            <el-icon><User /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="form.password" placeholder="请输入密码">
                        <template #prefix>
                            <el-icon><Lock /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button class="w-[250px]" round color="#626aef" type="primary" @click="onSubmit" :loading="loading">登 陆</el-button>
                </el-form-item>
            </el-form>

        </el-col>
    </el-row>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { showMessage } from '~/utils/my-notification'

const store = useStore()
const router = useRouter()

const form = reactive({
  username: '',
  password: ''
})

const rules = {
    username: [
        { required: true, message: '用户名不能为空!', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '密码不能为空!', trigger: 'blur' },
        { min: 6, max: 12, message: '长度为6到12个字符!', trigger: 'blur' }
    ]
}

const formRef = ref(null)
const loginLoding = ref(false)
const onSubmit = () => {
    formRef.value.validate((valid) => {
        if(!valid) {
            return false
        }
        loginLoding.value = true
        store.dispatch('login', form).then(res => {
            showMessage('登录成功!')
            // 跳转到后台首页
            router.push('/')
        }).catch(err => {
            console.log('err', err)
        }).finally(() => {
            loginLoding.value = false
        })
    })
}

</script>

<style>
    .login-container {
        @apply min-h-screen bg-indigo-500;
    }

    .left {
        @apply flex justify-center items-center;
    }

    .right {
        @apply bg-light-50 flex justify-center items-center flex-col;
    }
</style>
